package com.demo.bookstore.servlet;

import com.demo.bookstore.bean.User;
import com.demo.bookstore.service.UserService;
import com.demo.bookstore.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ddone
 * @date 2024/3/12-15:23
 */
@WebServlet("/registServlet")
public class RegistServlet extends HttpServlet {
    private final UserService userService = new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = new User();
        try {
            BeanUtils.populate(user,req.getParameterMap());
        } catch (Exception e) {
            e.printStackTrace();
        }
        int rows = userService.insertUser(user);
        if (rows > 0) {
            resp.sendRedirect(req.getContextPath()+"/pages/user/login.html");
        } else {
            req.getRequestDispatcher("/pages/user/regist.html").forward(req,resp);
        }
    }
}
